package models;
import java.text.Format;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.*;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import controllers.JsonDateFormatter;
import controllers.JsonDatetimeFormatter;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import controllers.Helper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
import models.Vehicle;
import models.Driver;
@Entity
@Table(name="insurance")
public class Insurance extends Model {
	private static final long serialVersionUID = 1L;
	@Id
	public Integer id; //编号
	@ManyToOne
	public Vehicle vehicle; //车号
	@ManyToOne
	public Driver driver; //司机
	
	public String insurance1; //保险1
	
	public Integer premiums1; //保费1
	
	public Integer insured1; //保额1
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date insured_date1; //投保日期1
	
	public String insurance2; //保险2
	
	public Integer premiums2; //保费2
	
	public Integer insured2; //保额2
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date insured_date2; //投保日期2
	
	public String insurance3; //保险3
	
	public Integer premiums3; //保费3
	
	public Integer insured3; //保额3
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date insured_date3; //投保日期3
	
	public String insurer_company; //保险公司
	
	public String contact; //保险公司联系人
	
	public Integer phone; //电话
	
	public Integer mphone; //手机
	
	public String insured; //投保联系人
	public static Model.Finder<Integer, Insurance> find = new Model.Finder<Integer, Insurance>(Integer.class, Insurance.class);

	public static Page<Insurance> page(int page, int pageSize, String sortBy, String order, String [] filterColum, String[] filter) {
		ExpressionList<Insurance> exp = find.where();
		for(int i= 0; i< filterColum.length; i++) {
			String[] tokens = filter[i].split("\\|", 3);
			if (tokens.length == 0) {
				continue;
			}
			String fieldtype = tokens[0];
			if (tokens.length == 3) { //two values, range search
				if (tokens[1].isEmpty() || tokens[2].isEmpty() ) {
					continue;
				}
				Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
				Object obj1 = Helper.stringToObject(fieldtype, tokens[2]);
				exp = exp.between(filterColum[i], obj0, obj1);
			} else if (tokens.length == 2) { //one value search
				if (tokens[1].isEmpty()) {
					continue;
				}
				if (fieldtype.equalsIgnoreCase("String")) {
					exp = exp.ilike(filterColum[i], "%" + tokens[1] + "%");
				} else {
					Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
					exp = exp.eq(filterColum[i], obj0);
				}
			}
		}
		return exp.orderBy(sortBy + " " + order).findPagingList(pageSize).setFetchAhead(false).getPage(page);
	}
	public void setId(Integer d) {
		id= d;
	}
	public Integer getId() {
		return id;
	}
	public void setVehicle(Vehicle d) {
		vehicle= d;
	}
	public Vehicle getVehicle() {
		return vehicle;
	}
	public void setDriver(Driver d) {
		driver= d;
	}
	public Driver getDriver() {
		return driver;
	}
	public void setInsurance1(String d) {
		insurance1= d;
	}
	public String getInsurance1() {
		return insurance1;
	}
	public void setPremiums1(Integer d) {
		premiums1= d;
	}
	public Integer getPremiums1() {
		return premiums1;
	}
	public void setInsured1(Integer d) {
		insured1= d;
	}
	public Integer getInsured1() {
		return insured1;
	}
	public void setInsured_date1(Date d) {
		insured_date1= d;
	}
	public Date getInsured_date1() {
		return insured_date1;
	}
	public void setInsurance2(String d) {
		insurance2= d;
	}
	public String getInsurance2() {
		return insurance2;
	}
	public void setPremiums2(Integer d) {
		premiums2= d;
	}
	public Integer getPremiums2() {
		return premiums2;
	}
	public void setInsured2(Integer d) {
		insured2= d;
	}
	public Integer getInsured2() {
		return insured2;
	}
	public void setInsured_date2(Date d) {
		insured_date2= d;
	}
	public Date getInsured_date2() {
		return insured_date2;
	}
	public void setInsurance3(String d) {
		insurance3= d;
	}
	public String getInsurance3() {
		return insurance3;
	}
	public void setPremiums3(Integer d) {
		premiums3= d;
	}
	public Integer getPremiums3() {
		return premiums3;
	}
	public void setInsured3(Integer d) {
		insured3= d;
	}
	public Integer getInsured3() {
		return insured3;
	}
	public void setInsured_date3(Date d) {
		insured_date3= d;
	}
	public Date getInsured_date3() {
		return insured_date3;
	}
	public void setInsurer_company(String d) {
		insurer_company= d;
	}
	public String getInsurer_company() {
		return insurer_company;
	}
	public void setContact(String d) {
		contact= d;
	}
	public String getContact() {
		return contact;
	}
	public void setPhone(Integer d) {
		phone= d;
	}
	public Integer getPhone() {
		return phone;
	}
	public void setMphone(Integer d) {
		mphone= d;
	}
	public Integer getMphone() {
		return mphone;
	}
	public void setInsured(String d) {
		insured= d;
	}
	public String getInsured() {
		return insured;
	}
	public static Insurance findById(Integer d) {
		return find.byId(d);
	}
	public static String getIdJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Insurance v: find.all()) {
			node.put(v.getId().toString(), v.getId().toString()); 
		}
		return node.toString();
	}
	public static Insurance findByInsurance1(String d) {
		return find.where().eq("insurance1", d).findUnique();
	}
	public static String getInsurance1Json() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Insurance v: find.all()) {
			node.put(v.getInsurance1().toString(), v.getInsurance1().toString()); 
		}
		return node.toString();
	}
	public static Insurance findByPremiums1(Integer d) {
		return find.where().eq("premiums1", d).findUnique();
	}
	public static String getPremiums1Json() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Insurance v: find.all()) {
			node.put(v.getPremiums1().toString(), v.getPremiums1().toString()); 
		}
		return node.toString();
	}
	public static Insurance findByInsured1(Integer d) {
		return find.where().eq("insured1", d).findUnique();
	}
	public static String getInsured1Json() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Insurance v: find.all()) {
			node.put(v.getInsured1().toString(), v.getInsured1().toString()); 
		}
		return node.toString();
	}
	public static Insurance findByInsured_date1(Date d) {
		return find.where().eq("insured_date1", d).findUnique();
	}
	public static String getInsured_date1Json() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Insurance v: find.all()) {
			node.put(v.getInsured_date1().toString(), v.getInsured_date1().toString()); 
		}
		return node.toString();
	}
	public static Insurance findByInsurance2(String d) {
		return find.where().eq("insurance2", d).findUnique();
	}
	public static String getInsurance2Json() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Insurance v: find.all()) {
			node.put(v.getInsurance2().toString(), v.getInsurance2().toString()); 
		}
		return node.toString();
	}
	public static Insurance findByPremiums2(Integer d) {
		return find.where().eq("premiums2", d).findUnique();
	}
	public static String getPremiums2Json() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Insurance v: find.all()) {
			node.put(v.getPremiums2().toString(), v.getPremiums2().toString()); 
		}
		return node.toString();
	}
	public static Insurance findByInsured2(Integer d) {
		return find.where().eq("insured2", d).findUnique();
	}
	public static String getInsured2Json() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Insurance v: find.all()) {
			node.put(v.getInsured2().toString(), v.getInsured2().toString()); 
		}
		return node.toString();
	}
	public static Insurance findByInsured_date2(Date d) {
		return find.where().eq("insured_date2", d).findUnique();
	}
	public static String getInsured_date2Json() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Insurance v: find.all()) {
			node.put(v.getInsured_date2().toString(), v.getInsured_date2().toString()); 
		}
		return node.toString();
	}
	public static Insurance findByInsurance3(String d) {
		return find.where().eq("insurance3", d).findUnique();
	}
	public static String getInsurance3Json() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Insurance v: find.all()) {
			node.put(v.getInsurance3().toString(), v.getInsurance3().toString()); 
		}
		return node.toString();
	}
	public static Insurance findByPremiums3(Integer d) {
		return find.where().eq("premiums3", d).findUnique();
	}
	public static String getPremiums3Json() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Insurance v: find.all()) {
			node.put(v.getPremiums3().toString(), v.getPremiums3().toString()); 
		}
		return node.toString();
	}
	public static Insurance findByInsured3(Integer d) {
		return find.where().eq("insured3", d).findUnique();
	}
	public static String getInsured3Json() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Insurance v: find.all()) {
			node.put(v.getInsured3().toString(), v.getInsured3().toString()); 
		}
		return node.toString();
	}
	public static Insurance findByInsured_date3(Date d) {
		return find.where().eq("insured_date3", d).findUnique();
	}
	public static String getInsured_date3Json() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Insurance v: find.all()) {
			node.put(v.getInsured_date3().toString(), v.getInsured_date3().toString()); 
		}
		return node.toString();
	}
	public static Insurance findByInsurer_company(String d) {
		return find.where().eq("insurer_company", d).findUnique();
	}
	public static String getInsurer_companyJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Insurance v: find.all()) {
			node.put(v.getInsurer_company().toString(), v.getInsurer_company().toString()); 
		}
		return node.toString();
	}
	public static Insurance findByContact(String d) {
		return find.where().eq("contact", d).findUnique();
	}
	public static String getContactJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Insurance v: find.all()) {
			node.put(v.getContact().toString(), v.getContact().toString()); 
		}
		return node.toString();
	}
	public static Insurance findByPhone(Integer d) {
		return find.where().eq("phone", d).findUnique();
	}
	public static String getPhoneJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Insurance v: find.all()) {
			node.put(v.getPhone().toString(), v.getPhone().toString()); 
		}
		return node.toString();
	}
	public static Insurance findByMphone(Integer d) {
		return find.where().eq("mphone", d).findUnique();
	}
	public static String getMphoneJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Insurance v: find.all()) {
			node.put(v.getMphone().toString(), v.getMphone().toString()); 
		}
		return node.toString();
	}
}
